package org.hong.monkey.rdd

import org.hong.monkey.crawl.CrawlRequest
import org.hong.monkey.{Partition, TaskContext}

import scala.reflect.ClassTag

/**
 * Created by wangsihong on 2016/10/31.
 */
private[monkey] class CrawlMapPartitionsRDD[T: ClassTag] (
  var prev: RDD[T],
  f: (TaskContext, Int, Iterator[T]) => Iterator[CrawlRequest],  // (TaskContext, partition index, iterator)
  preservesPartitioning: Boolean = false)
  extends RDD[CrawlRequest](prev) {

  override val partitioner = if (preservesPartitioning) firstParent[T].partitioner else None

  override def getPartitions: Array[Partition] = firstParent[T].partitions

  override def compute(split: Partition, context: TaskContext): Iterator[CrawlRequest] =
    f(context, split.index, firstParent[T].iterator(split, context))

  override def clearDependencies() {
    super.clearDependencies()
    prev = null
  }
}
